//
//  ZFMapViewController.m
//  外星球生活圈
//
//  Created by apple on 15/8/17.
//  Copyright (c) 2015年 MR.ZHOU. All rights reserved.
//

#import "ZFMapViewController.h"
#import <MapKit/MapKit.h>
#import "ZFMapICon.h"
#import "ZFMapView.h"
#import <CoreLocation/CoreLocation.h>
@interface ZFMapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

@property (strong, nonatomic) CLLocationManager *manager;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end



@implementation ZFMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // iOS8版本判断
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        _manager = [CLLocationManager new];
        [_manager setDelegate:self];
        [_manager requestAlwaysAuthorization];
    }
    CGFloat latitude = 23.133916 ;
    CGFloat longitude = 113.395557;
    CLLocationCoordinate2D currentdate = CLLocationCoordinate2DMake(latitude, longitude);
    
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    self.mapView.mapType =  MKMapTypeStandard;
}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        NSLog(@"授权申请成功");
    }
}


#pragma mark - MKMapViewDelegate

/**
 *  地图视图加载完成之后调用
 *
 *  @param mapView 地图视图
 */
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
    NSLog(@"mapViewDidFinishLoadingMap");
}

/**
 *  当MapView更新到用户位置是触发
 *
 *  @param mapView      触发的对象
 *  @param userLocation 用户的当前位置
 */
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    NSLog(@"%@, %@", userLocation.title, userLocation.location);
    
    CGFloat latitude = 23.133916;
    CGFloat longitude = 113.395557;
    CLLocationCoordinate2D currentdate = CLLocationCoordinate2DMake(latitude, longitude);
    // 设置地图显示的范围
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5,0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(currentdate, span);
    [_mapView setRegion:region animated:YES];
    
    //    self.userLocation = userLocation;
}

// 地图显示区域即将发生改变时触发
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
    NSLog(@"区域要改变");
}

// 地图显示区域已经完成改变是触发
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"区域已经改变完成 %f, %f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    if ([annotation isKindOfClass:[ZFMapICon class]]) {
        
        ZFMapView *view = [ZFMapView annotationViewMapView:self.mapView withAnnotation:(ZFMapICon *)annotation];
        
        return view;
    }
    
    // 系统默认的样式
    return nil;
}

//点击按钮
- (IBAction)clickButton:(id)sender {
    CGFloat latitude = 23.133916 + arc4random_uniform(5) * 0.0001;
    CGFloat longitude = 113.395557  + arc4random_uniform(5) * 0.0001;
    CLLocationCoordinate2D currentdate = CLLocationCoordinate2DMake(latitude, longitude);
    // 点击按钮的时候添加一个大头针模型
   ZFMapICon  *annotation = [ZFMapICon annotationWithTitle:@"陈家林" subtitle:@"山清水秀的地方" setimage:[UIImage imageNamed:@"category_1"]];
    annotation.coordinate = currentdate;
    NSLog(@"进来了");
    // 添加大头针模型
    [self.mapView addAnnotation:annotation];
}

@end
